package core.containers
{
	/**
	 * @author lonely
	 * @date Feb 5, 2013
	 */
	import core.Util;
	import core.events.GameEvent;
	
	import flash.display.Sprite;
	import flash.text.TextField;
	
	public class ChipEffectContainer extends Sprite
	{
		
		private var container:Sprite;
		
		private var textField:CardPointTextField;
		
		private var enabledTextField:Boolean;
		
		public function ChipEffectContainer(enabledTextField:Boolean = false)
		{
			super();
			this.mouseChildren = this.mouseEnabled = false;
			this.enabledTextField = enabledTextField;
			this.container = new Sprite();
			this.addChild(container);
			if(this.enabledTextField){
				this.textField = new CardPointTextField();
				this.addChild(this.textField);
				this.textField.x = -20;
				this.textField.y = 30;
			}
			this.scaleX = this.scaleY = 0.5;
		}
		
		private var _total:Number = 0;
		
		public function get total():Number {
			return this._total;
		}
		
		public function add(value:Array):void {
			this.removeAllChindren();
			var yy:Number = 0;
			var vGap:Number = 5;
			for(var i:int = 0; i < value.length; i++){
				var temp:Object = value[i];
				for(var j:int = 0; j < temp.r; j++){
					var chipEffect:ChipEffect = new ChipEffect();
					chipEffect.mouseEnabled = false;
					chipEffect.mouseChildren = false;
					chipEffect.data = temp.v;
					chipEffect.y = yy;
					yy -= vGap;
					this.container.addChild(chipEffect);
					this._total += temp.v.value;
				}
			}	
			if(this.textField){
				if(this._total == 0){
					this.textField.visible = false;
					this.dispatchEvent(new GameEvent(GameEvent.UPDATE_PLAYER_TABLE));
				}else {
					this.textField.text = "" + this._total;
					this.textField.visible = true;
				}
			}
		}
		
		
		/**
		 * public function add(value:Number):void {
			var list:Array = Util.split(value);
			this.removeAllChindren();
			var yy:Number = 0;
			var vGap:Number = 5;
			for(var i:int = 0; i < list.length; i++){
				var temp:Object = list[i];
				for(var j:int = 0; j < temp.r; j++){
					var chipEffect:ChipEffect = new ChipEffect();
					chipEffect.mouseEnabled = false;
					chipEffect.mouseChildren = false;
					chipEffect.data = temp.v;
					chipEffect.y = yy;
					yy -= vGap;
					this.container.addChild(chipEffect);
					this._total += temp.v.value;
				}
			}	
			if(this.textField){
				this.textField.text = "" + this._total;
				this.textField.visible = true;
			}
		}
		 */ 
		
		public function removeAllChindren():void {
			while(this.container.numChildren > 0){
				this.container.removeChildAt(0);
			}
			if(this.textField){
				this.textField.visible = false;
			}
			this._total = 0;
			this.dispatchEvent(new GameEvent(GameEvent.UPDATE_PLAYER_TABLE));
		}
		
		public function get data():Object {
			return {value:this._total};
		}
		
		public function dispose():void {
			this.removeAllChindren();
			if(this.parent){
				this.parent.removeChild(this);
			}
		}
		
		public function clone():ChipEffectContainer {
			var result:ChipEffectContainer = new ChipEffectContainer();
			result.add(Util.split(this._total));
			return result;
		}
	}
}